<template>
  <a-tree-select
    :allowClear="allowClear"
    :disabled="disabled"
    :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
    :tree-data="options"
    @change="changeHandler"
    :placeholder="placeholder"
    tree-default-expand-all
    v-model="value"
  >
  </a-tree-select>
</template>

<script>
export default {
  model: {
    prop: 'outValue',
    event: 'change'
  },
  props: {
    outValue: {
      required: true
    },
    disabled: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      required: false,
      default: ''
    },
    expandTrigger: {
      type: String,
      required: false,
      default: 'hover'
    },
    allowClear: {
      type: Boolean,
      required: false,
      default: false
    },
    replaceFields: {
      type: Object,
      required: false,
      default: () => {
        return { children: 'children', title: 'name', key: 'id', value: 'id' }
        // return { label: 'name', value: 'id', children: 'children' }
      }
    }
  },
  data () {
    return {
      value: null,
      options: []
    }
  },
  watch: {},

  mounted () {
    this.convertOutValue()
    this.getOptions()
  },
  methods: {
    convertOutValue () {
      if (this.outValue === null) {
        this.value = null
      } else {
        this.value = +this.outValue
      }
    },

    // 获取数据
    getOptions () {
      this.$axios
        .get(this.$URLMAP.url.dept.query)
        .then((response) => {
          if (response.code === 200) {
            // let options = this.$util.filterRowChildren(response.data)
            const options = response.data
            this.options = this.$util.treeTransition(
              options, { label: 'name', key: 'id', value: 'id' }
            )
          }
        })
    },

    changeHandler (val) {
      if (val == null || val.length === 0) {
        this.$emit('change', null)
      } else {
        this.$emit('change', val)
      }
    }
  }
}
</script>
